package com.ynxd56.westar.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;


import com.ynxd56.westar.pojo.Ticket;

public class TicketDao {

	public List<Ticket> findByPlanId(Long planId){
		List<Ticket> list=new ArrayList<Ticket>();
		Ticket ticket=null;
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try{
			String sql="select a.*,b.name from t_book_ticket as a left join t_place_info as b on a.scenic_id = b.id where a.plan_id = ?";
			conn = DBConnection.getConnection();
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, String.valueOf(planId));
			rs = stmt.executeQuery();
			while(rs.next()){
				ticket=new Ticket();
				ticket.setId(rs.getInt("id"));
				ticket.setDate(rs.getDate("date"));
				ticket.setTime(rs.getTimestamp("time"));
				ticket.setHour(rs.getInt("hour"));
				ticket.setScenicId(rs.getInt("scenic_id"));
				ticket.setPrice(rs.getFloat("price"));
				ticket.setTotal(rs.getInt("total"));
				ticket.setSelf(rs.getInt("self"));
				ticket.setNote(rs.getString("note"));
				ticket.setScenicName(rs.getString("name"));
				list.add(ticket);
			}
		}catch(Exception e){
			e.printStackTrace(); 
		}finally{
			DBConnection.closeResultSet(rs);
			DBConnection.closeStatement(stmt);
			DBConnection.closeConnection(conn);
		}
		return list;
	}
}
